const basePage = require('../../common/BasePage')
Page({
  behaviors: [basePage],
  data: {
    latitude: "",
    longitude: "",
    markers: [],
    dataArr: [],
    areaName: "",
    goodsSid: "",
    firstLevelSid: ""
  },
  onShow() {
    this.getTabBar().init();
    let globalData = getApp().globalData;
    // console.log('globalData', globalData);
    if (globalData.latitude) {
      this.setData({
        latitude: globalData.latitude,
        longitude: globalData.longitude,
        areaName: globalData.areaName
      })
    }
    this.get_stores();
  },

  onLoad(obj) {
    // console.log('附近', obj);
    this.data.firstLevelSid = obj.firstLevelSid || "";
    this.data.goodsSid = obj.goodsSid || '';
  },
  get_stores() {
    if (this.data.latitude && this.data.longitude) {
      return this.req_store()
    }
    wx.getLocation({
      altitude: true,
      highAccuracyExpireTime: 0,
      isHighAccuracy: true,
      type: "gcj02",
      success: (res) => {
        getApp().globalData.latitude = res.latitude;
        getApp().globalData.longitude = res.longitude;
        this.setData({
          latitude: res.latitude,
          longitude: res.longitude
        })
        this.req_store();
        this.req_city()
      },
      fail: (res) => {
        wx.showToast({
          title: '获取当前位置失败',
          icon: 'none',
        })
      },
    })
  },
  area_click() {
    wx.chooseLocation({
      latitude: this.data.latitude * 1,
      longitude: this.data.longitude * 1,
      success: (result) => {
        // console.log('chooseLocation', result);
        this.data.latitude = getApp().globalData.latitude = result.latitude;
        this.data.longitude = getApp().globalData.longitude = result.longitude;
        let areaName = getApp().globalData.areaName = result.name || result.address;
        this.setData({
          areaName: areaName
        })
        this.req_store();
      },

    })
  },
  req_city() {
    this.request({
      url: this.data.host + "/v1/user-address/city",
      method: 'post',
      data: {
        location: this.data.latitude + "," + this.data.longitude
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      }
    }, res => {

      let areaName = getApp().globalData.areaName = area.street || area.district || area.city || area.province;
      this.setData({
        areaName: areaName
      })
    })
  },
  req_store() {
    this.request({
      url: this.data.host + "/v1/stores/nearby-store",
      data: {
        latitude: this.data.latitude,
        longitude: this.data.longitude,
        size: 50,
        goodsSid: this.data.goodsSid
      }
    }, res => {
      let arr = [];
      for (let i = 0; i < res.data.records.length; i++) {
        const val = res.data.records[i];
        arr.push({
          id: i,
          latitude: val.latitude,
          longitude: val.longitude,
          iconPath: '/assets/store-map.png',
          width: 50,
          height: 50,
          customCallout: {
            anchorY: 0,
            anchorX: 20,
            display: 'ALWAYS',
          },
        })
      }

      this.setData({
        markers: arr,
        dataArr: res.data.records
      })

    })
  },
  dh_click(e) {
    console.log(e);
    let item = e.currentTarget.dataset.item;
    wx.openLocation({
      latitude: item.latitude * 1,
      longitude: item.longitude * 1,

      success: (res) => {},
      fail: (res) => {
        console.log('打开地图', res);
      },
      complete: (res) => {},
    })
  },
  store_click(e) {
    // console.log("store_click", e);

    wx.setStorageSync('store', e.currentTarget.dataset.item);
    wx.navigateTo({
      url: '/pages/goods/search/index?storeSid=' + e.currentTarget.dataset.item.sid + "&firstLevelSid=" + this.data.firstLevelSid,
    })
  },
  lxkf_click(e) {
    console.log('lxkf_click', e.currentTarget.dataset.item.customerUrl);
    wx.openCustomerServiceChat({
      corpId: 'ww72ede85652b45a26',
      extInfo: {
        url: e.currentTarget.dataset.item.customerUrl,
      },
      success: (res) => {
        console.log('打开成功', res);
      },
      fail: (res) => {
        console.log('打开失败', res);
        wx.showToast({
          title: res.errMsg,
          icon: 'none'
        })
      },

    })
  },

});